package com.navi.alarm.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Util {
	private static String strMySQLTimeFormat = "yyyy-MM-dd HH:mm:ss";

	public static String stackTraceToString(Exception excp) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw, true);
		excp.printStackTrace(pw);
		pw.flush();
		sw.flush();
		return sw.toString();
	}

	public static String getEvtTime() {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String evt_timestamp = tempDate.format(now.getTime());
		return evt_timestamp;
	}

	public static Timestamp getCurrentTimestamp() {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String evt_timestamp = tempDate.format(now.getTime());
		Timestamp timestatmp = null;
		try {
			timestatmp = Timestamp.valueOf(evt_timestamp);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return timestatmp;
	}

	public static String getEvtTimeforVer() {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMdd");
		String evt_timestamp = tempDate.format(now.getTime());
		return evt_timestamp;
	}

	public static String getRvTime() {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
		String evt_timestamp = tempDate.format(now.getTime());
		return evt_timestamp;
	}

	public static String getMillTimeStr() {
		Calendar now = Calendar.getInstance();
		// SimpleDateFormat tempDate = new
		// SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
		SimpleDateFormat tempDate = new SimpleDateFormat(strMySQLTimeFormat);
		String evt_timestamp = tempDate.format(now.getTime());
		return evt_timestamp;
	}

	public static Date getMillTime() {// TODO Need format
		return String2Date(getMillTimeStr());
	}

	public static String formatEvtTime(Calendar calendar) {
		SimpleDateFormat tempDate = new SimpleDateFormat(strMySQLTimeFormat);
		String evt_timestamp = tempDate.format(calendar.getTime());
		return evt_timestamp;
	}

	public static String formatRvTime(Calendar calendar) {
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
		String evt_timestamp = tempDate.format(calendar.getTime());
		return evt_timestamp;
	}

	public static Date String2Date(String dateString) {
		Date date = new Date();

		try {
			SimpleDateFormat sdf = new SimpleDateFormat(strMySQLTimeFormat);
			date = sdf.parse(dateString);
		} catch (ParseException e) {
			System.out.println(e.getMessage());
		}
		return date;
	}

	public static Date String2Time(String timeString) {
		Date date = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("HH.mm.ss");
			date = sdf.parse(timeString);
		} catch (ParseException e) {
			System.out.println(e.getMessage());
		}
		return date;
	}

	public static Timestamp String2Timestamp(String timeString) {
		Timestamp timestatmp = null;
		try {
			timestatmp = Timestamp.valueOf(timeString);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return timestatmp;
	}

	/**
	 * Get time diff value (By min)
	 * 
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static long getDiffMin(String startTime, String endTime) {
		long diff;
		long diffMin = 0;
		long nm = 1000 * 60;// 一分钟的毫秒数
		SimpleDateFormat sd = new SimpleDateFormat(strMySQLTimeFormat);
		try {
			// 获得两个时间的毫秒时间差异
			diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
			diffMin = diff / nm;// 计算差多少分钟
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return diffMin;
	}

	/**
	 * Get time diff value (By min)
	 * 
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static long getDiffMin(Timestamp startTime, Timestamp endTime) {
		long diffMin = 0;
		long nm = 1000 * 60;// 一分钟的毫秒数

		long diff = endTime.getTime() - startTime.getTime();
		diffMin = diff / nm;// 计算差多少分钟 //TODO Double

		return diffMin;
	}

	public static boolean isSpaceCheck(String str) {
		if (null == str || str.length() <= 0 || ("").equals(str)) {
			return true;
		}
		return false;
	}

}
